package com.llk.pool.mapper.sql;

import com.llk.pool.domain.User;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by zhou on 17-5-2.
 */
public class UserMapperProvider {

    private Logger logger = LoggerFactory.getLogger(UserMapperProvider.class);

    /**
     * update users set userName=#{userName}, passWord=#{passWord}, user_sex=#{userSex},nick_name=#{nickName} where id=#{id}
     *
     * @param user
     * @return
     */
    public String update(User user) {
        SQL sql = new SQL();
        sql.UPDATE("users");

        if (user.getUserName() != null)
            sql.SET("username='" + user.getUserName() + "'");
        if (user.getPassWord() != null)
            sql.SET("passWord='" + user.getPassWord() + "'");
        if (user.getUserSex() != null)
            sql.SET("user_sex='" + user.getUserSex() + "'");
        if (user.getNickName() != null)
            sql.SET("nick_name='" + user.getNickName() + "'");


        sql.WHERE("id=" + user.getId());

        logger.info(sql.toString());

        return sql.toString();
    }

    /**
     * 设置 auto_increment
     *
     * @return
     */
    public String setAutoIncrement(Long id) {

        return "alter table users auto_increment = " + id;
    }

    /**
     * 获取当前auto_increment
     *
     * @return
     */
    public String getAutoIncrement() {
        //return "select auto_increment from information_schema.tables where table_name='users' limit 1 ,1 "; //linux
        return "select auto_increment from information_schema.tables where table_name='users'";//windows
    }

    /**
     * 获取总记录数
     *
     * @return
     */
    public String getCount() {
        return " select count(id) from users ";
    }

    /**
     * 按条件查询
     * @param pageSize
     * @param startRecord
     * @param sortColumn
     * @param sortDir
     * @param searchValue
     * @return
     */
    public String loadUserList(int pageSize, int startRecord, String sortColumn, String sortDir, String searchValue) {
        StringBuffer sql = new StringBuffer("select * from users ");


        //for search
        String[] columnsName = {"id", "userName", "nick_name", "user_sex"};
        boolean searchAble = false;
        if (searchValue != null && !"".equals(searchValue)) {
            sql.append("where ");
            searchAble = true;
        }

        if (searchAble) {
            StringBuffer temp = new StringBuffer();
            for (String column : columnsName) {
                temp.append(column + " like '%" + searchValue + "%' or ");
            }
            sql.append(temp.substring(0, temp.length() - 3));
        }

        //for order
        sql.append(" order by " + sortColumn + " " + sortDir + "");


        //for pagination
        //sql.append(" limit ?,? ");

        sql.append(" limit " + startRecord + ", " + (startRecord + pageSize));//这样不安全，下次改成参数的，先这样
        System.out.println(sql.toString());
        logger.info("SQL : " + sql.toString());

        return sql.toString();
    }





}
